package org.boulderfoodrescue.webapp.client.multipleaddresspanel;

import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class AddressCellTable extends FlowPanel{
  protected CellTable<Placemark> table;
  protected ListDataProvider<Placemark> dataProvider = new ListDataProvider<Placemark>();
  public AddressCellTable(final MultipleAdressDialog dlg){
    table = new CellTable<Placemark>();
    TextColumn<Placemark> nameColumn = new TextColumn<Placemark>(){
      @Override
      public String getValue(Placemark object) {
        return object.getAddress();
      }
    };
    table.addColumn(nameColumn, "Address");
    
    dataProvider.addDataDisplay(table);
    
    final SingleSelectionModel<Placemark> selectionModel = new SingleSelectionModel<Placemark>();
    table.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      @Override
      public void onSelectionChange(SelectionChangeEvent event) {
        Placemark selected = selectionModel.getSelectedObject();
        dlg.onLocationSelection(selected);
      }
    });
    
    this.add(table);
  }
  public void addPlacemark(Placemark place){
    dataProvider.getList().add(place);
  }
  public CellTable<Placemark> getTable() {
    return table;
  }
  public void setTable(CellTable<Placemark> table) {
    this.table = table;
  }
  public ListDataProvider<Placemark> getDataProvider() {
    return dataProvider;
  }
  public void setDataProvider(ListDataProvider<Placemark> dataProvider) {
    this.dataProvider = dataProvider;
  }
}
